package com.zbb.user.service.impl;


import com.sun.xml.bind.v2.runtime.unmarshaller.LocatorEx;
import com.zbb.base.model.Result;
import com.zbb.user.feignclient.OrderSnapshotServiceClient;
import com.zbb.user.mapper.CreditMapper;
import com.zbb.user.mapper.UserMapper;
import com.zbb.user.model.dto.CommentDto;
import com.zbb.user.model.dto.UserLoginDto;
import com.zbb.user.model.po.Credit;
import com.zbb.user.model.po.User;
import com.zbb.user.service.CreditService;
import lombok.extern.slf4j.Slf4j;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Comment;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * <p>
 * 信誉表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CreditServiceImpl implements CreditService {
    @Autowired
    UserMapper userMapper;


    @Autowired
    CreditMapper creditMapper;

    @Autowired
    OrderSnapshotServiceClient orderSnapshotServiceClient;


    @Transactional
    @Override
    public void insertCredit(CommentDto commentDto) {
        //需要完成
//        1.根据userId=id查询zbb_user库中的user表获取被评价用户的当前评分credit_score。
                User user = userMapper.selectById(commentDto.getUserId());
                float credit = (user.getCreditScore()+commentDto.getGrade())/2;
//        2.得到grade评分与credit_score相加/2得到的分数即当前用户分数，然后把当前用户分数更新到user表中的credit_score字段。
        //        3.存储评论数据，credit_score存更新前的分数，到zbb_user库中的user_credit表中，
                Credit credit1 = new Credit(UUID.randomUUID().toString(),commentDto.getUserId(),commentDto.getCommentUserId(),commentDto.getOrderSnapshotId(),commentDto.getComment(),commentDto.getGrade(),user.getCreditScore(),LocalDateTime.now());
                user.setCreditScore(credit);
                userMapper.updateById(user);
                creditMapper.insert(credit1);
//        4.根据orderSnapshotId的值=id更新zbb_order库中的表zbb_order_snapshot中的evaluation_status的状态为evaluationStatus。

            orderSnapshotServiceClient.updateEvaluationStatus(commentDto.getOrderSnapshotId(),commentDto.getEvaluationStatus());

    }
}
